<template>
  <div>
    <el-table :data="goodsList" border style="width: 100%">
      <el-table-column prop="id" label="商品编号" width="100">
      </el-table-column>
      <el-table-column prop="goodsname" label="商品名称" width="120">
      </el-table-column>
      <el-table-column prop="price" label="商品价格"> </el-table-column>
      <el-table-column prop="market_price" label="市场价格"> </el-table-column>
      <!-- 图片 -->
      <el-table-column prop="img" label="图片" width="120">
        <template slot-scope="item">
          <div>
            <img v-if="item.row.img" :src="item.row.img" class="img" alt="" />
          </div>
        </template>
      </el-table-column>

      <el-table-column prop="isnew" label="是否新品">
        <template slot-scope="item">
          <div>
            <el-tag effect="dark" type="success" v-if="item.row.isnew == 1"
              >是</el-tag
            >
            <el-tag effect="dark" type="warning" v-else>否</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="ishot" label="是否热卖">
        <template slot-scope="item">
          <div>
            <el-tag effect="dark" type="success" v-if="item.row.ishot == 1"
              >是</el-tag
            >
            <el-tag effect="dark" type="warning" v-else>否</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态">
        <template slot-scope="item">
          <div>
            <el-button
              type="success"
              plain
              size="small"
              v-if="item.row.status == 1"
              >启用</el-button
            >
            <el-button type="warning" plain size="small" v-else>禁用</el-button>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="address" label="操作">
        <template slot-scope="item">
          <div>
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              circle
              @click="edit(item.row.id)"
            ></el-button>
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-delete"
              circle
              @click="del(item.row.id)"
            ></el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      background
      layout="prev, pager, next"
      @current-change="changepage"
      :page-size="pageNum"
      :total="count"
      style="margin-top: 15px; float: right; margin-right: 10px"
    >
    </el-pagination>
  </div>
</template>

<script>
import { mapGetters, mapActions } from "vuex";
import { goodsdelete } from "../../require/api";
export default {
  data() {
    return {};
  },
  methods: {
    ...mapActions({
      getManageAction: "manage/getManageAction",
      getCountAction: "manage/getCountAction",
      changePageAction: "manage/changePageAction",
    }),
    edit(id) {
      this.$emit("edit", id);
    },
    del(id) {
      this.$confirm("你确定要删除这条数据吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 接口删除
          goodsdelete({ id }).then((res) => {
            if (res.code == 200) {
              this.getManageAction();
              this.getCountAction();
              //成功提示
              this.$message.success(res.msg);
            } else {
              //失败提示
              this.$message.error(res.msg);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //分页点击的当前页
    changepage(p) {
      // console.log(p);
      // 传参给更改页码切换页面的方法
      this.changePageAction(p);
    },
  },
  computed: {
    ...mapGetters({
      goodsList: "manage/getGoods",
      count: "manage/getCount",
      pageNum: "manage/getpageNum",
    }),
  },
  mounted() {
    this.getManageAction();
    this.getCountAction();
  },
};
</script>
<style lang=less scoped >
.img {
  width: 80px;
  height: 80px;
}
</style>
